#include "q_data.h"
#include "q_seek.h"
#include "ui_q_seek.h"
#include <QDebug>
#include <QSqlQuery>

Q_seek::Q_seek(QWidget* parent) :
    QWidget(parent),
    ui(new Ui::Q_seek)
{
    ui->setupUi(this);


}

Q_seek::~Q_seek()
{
    delete ui;
}

void Q_seek::on_pushButtonSeek_clicked()
{
    QString info = ui->lineEditSeekName->text();
    QSqlQuery query;
    query.prepare("SELECT * FROM wenjuan WHERE name LIKE ? ");
    query.addBindValue("%" + info + "%");
    if(query.exec())
    {
        qDebug() << "更新成功！";
        ui->tableWidget_2->clearContents();
        ui->tableWidget_2->setRowCount(0);
        while(query.next())
        {
            QString name = query.value(0).toString();
            QString age = query.value(1).toString();
            QString banji = query.value(2).toString();
            QString hoppy = query.value(3).toString();
            qDebug() << QString("name %1 age %2 banji 3% hoppy 4% ").arg(name).arg(age).arg(banji).arg(hoppy);

            QTableWidgetItem* nameItem = new QTableWidgetItem(name);
            QTableWidgetItem* ageItem = new QTableWidgetItem(age);
            QTableWidgetItem* banjiItem = new QTableWidgetItem(banji);
            QTableWidgetItem* hoppyItem = new QTableWidgetItem(hoppy);


            int row = ui->tableWidget_2->rowCount();
            ui->tableWidget_2->insertRow(row);
            ui->tableWidget_2->setItem(row, 0, nameItem);
            ui->tableWidget_2->setItem(row, 1, ageItem);
            ui->tableWidget_2->setItem(row, 2, banjiItem);
            ui->tableWidget_2->setItem(row, 3, hoppyItem);
        }
    }
    else
    {
        qDebug() << "更新失败！";
    }
}

void Q_seek::on_pushButton_clicked()
{
    Q_data* m = new Q_data();
    m->show();
    this->close();
}
